package wyklad;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class LicznikNaSesji extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf8");
        PrintWriter pw = response.getWriter();

        pw.println("<html>");
        pw.println("<head>");
        pw.println("</head>");
        pw.println("<body>");
        pw.println("<h3>LicznikNaSesji</h3>");

        HttpSession session = request.getSession();

        //zad2
        synchronized (session) {
            Integer i = (Integer) session.getAttribute("wyklad.LicznikNaSesji.i");
            if (i == null)
                i = new Integer(0);
            else
                i++;
            session.setAttribute("wyklad.LicznikNaSesji.i", i);
            pw.println("aktualna wartość: " + i++);
            pw.println("<h3>Dane sesji</h3>");
            Enumeration sesNames = session.getAttributeNames();
            while (sesNames.hasMoreElements()) {
                String name = sesNames.nextElement().toString();
                Object value = session.getAttribute(name);
                pw.println(name + " = " + value + "<br>");
            }
            pw.println("<h3> Statystyka </h3>");
            pw.println("getID: " + session.getId() + "<br/>");
            pw.println("isNew: " + session.isNew() + "<br/>");
            pw.println("getCreationTime: " + session.getCreationTime());
            pw.println("<i>(" + new Date(session.getCreationTime()) + ")</i><br/>");
            pw.println("getLastAccessedTime: " + session.getLastAccessedTime());
            pw.println("<i>(" + new Date(session.getLastAccessedTime()) + ")</i><br/>");
            pw.println("getMaxInactiveInterval: " + session.getMaxInactiveInterval() + "<br/>");
            pw.println("getRequestedSessionId: " + request.getRequestedSessionId() + "<br/>");
            pw.println("isRequestedSessionIdFromCookie: " + request.isRequestedSessionIdFromCookie() + "<br/>");
            pw.println("isRequestedSessionIdFromURL: " + request.isRequestedSessionIdFromURL() + "<br/>");
            pw.println("isRequestedSessionIdValid: " + request.isRequestedSessionIdValid() + "<br/>");
        }
        pw.println("</body>");
        pw.println("</html>");
        pw.close();
    }
}